/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package io.github.ilkgunel.operations;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import io.github.ilkgunel.database.AccessMongoDB;
import io.github.ilkgunel.pojo.Address;
import io.github.ilkgunel.pojo.Record;
import io.github.ilkgunel.pojo.Student;
import org.bson.Document;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Double.parseDouble;

/**
 *
 * @author ilkaygunel
 */
@WebServlet(name = "test2_1", urlPatterns = {"/test2_1"})
public class test2_1 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String operationMessage = "";
        HttpSession session = request.getSession();
        operationMessage = (String) session.getAttribute("operationMessage");

        if (operationMessage == null || operationMessage.equals("")) {
            operationMessage = "";
        }
        session.removeAttribute("operationMessage");

        List<Student> list = new ArrayList<>();
        //FindIterable<Document> result = getMongoDatabase().getCollection("Records").find(new Document("name","Can"));
        AccessMongoDB accessMongoDB = new AccessMongoDB();


        Document filter = new Document();
       /* //小于 20岁的
         filter.append("age",new Document("$lt", 20));
*/
        FindIterable<Document> result = accessMongoDB.getCollection("student").find();



        MongoCursor<Document> cursor = result.iterator();

        while (cursor.hasNext()) {

            Document document = cursor.next();

           /* System.out.println(document.get("_id").toString());
            System.out.println(new BigDecimal((document.get("sid")).toString()).toPlainString());
            System.out.println(document.get("name").toString());
            System.out.println( document.get("sex").toString());
            String temp= new BigDecimal((document.get("age")).toString()).toPlainString();
            System.out.println((int)Double.parseDouble( temp));
            System.out.println(document.get("birthday").toString());
            System.out.println(document.get("dname").toString());
            temp= new BigDecimal((document.get("class")).toString()).toPlainString();
            System.out.println((int) Double.parseDouble( temp));*/
            String temp;
            Student s=new Student();
            s.set_id(document.get("_id").toString());

            s.setSid( new BigDecimal((document.get("sid")).toString()).toPlainString());
            s.setName(document.get("name").toString());
            s.setSex( document.get("sex").toString());

             temp= new BigDecimal((document.get("age")).toString()).toPlainString();
            s.setAge((int)Double.parseDouble( temp));

            s.setBirthday( document.get("birthday").toString());
            s.setDname( document.get("dname").toString());

            s.setTheclass( document.get("class").toString());


           /* Address address1 = new Address();
            address1.setStreet(( address.get("street"));
            address1.setBorough((String) address.getString("borough"));
            address1.setCity((String) address.getString("city"));

            Record record = new Record();
            record.setId(document.get("_id").toString());
            record.setName((String) document.get("name"));
            record.setSurname((String) document.get("surname"));
            record.setAddress(address1);
*/
            list.add(s);
        }

        session.setAttribute("Student", list);
        session.setAttribute("operationMessage", operationMessage);

        accessMongoDB.closeMongoClient();

        RequestDispatcher view = request.getRequestDispatcher("/test2_1.jsp");
        view.forward(request, response);
    }

}
